package controllers

import (
	"encoding/json"
	"onion/models"
	"onion/utils/tool"

	"github.com/astaxie/beego/orm"
)

type SoftController struct {
	BaseController
}

type AppParam struct {
	Name            string `json:"name"`
	HeartTime       int    `json:"heart_time"`
	Version         string `json:"version"`
	Notice          string `json:"notice"`
	StaticData      string `json:"static_data"`
	GiveTime        int    `json:"give_time"`
	LoginType       int8   `json:"login_type"`
	UpdateData      string `json:"update_data"`
	UpdateType      int8   `json:"update_type"`
	TrialInterval   int    `json:"trial_interval"`
	TrialData       int    `json:"trial_data"`
	SoftwareState   int8   `json:"software_state"`
	BindingType     int8   `json:"binding_type"`
	BindingdelType  int8   `json:"bindingdel_type"`
	BindingdelTime  int    `json:"bindingdel_time"`
	RestrictRegtime int    `json:"restrict_regtime"`
	RestrictRegnum  int    `json:"restrict_regnum"`
	Remote          string `json:"remote"`
	Encrypt         string `json:"encrypt"`
}

// AddSoft 添加应用
func (this *SoftController) AddSoft() {
	var app AppParam
	var err error
	if err = json.Unmarshal(this.Ctx.Input.RequestBody, &app); err == nil {
		o := orm.NewOrm()
		v := models.BxSoftware{}
		v.Name = app.Name
		if err = o.Read(&v, "name"); err == nil {
			this.ErrorJson(tool.SOFTWAREERR, "该应用以存在")
			return
		}
		v.Name = app.Name
		v.HeartTime = app.HeartTime
		v.Version = app.Version
		v.Notice = app.Notice
		v.StaticData = app.StaticData
		v.GiveTime = app.GiveTime
		v.LoginType = app.LoginType
		v.UpdateData = app.UpdateData
		v.UpdateType = app.UpdateType
		v.TrialInterval = app.TrialInterval
		v.TrialData = app.TrialData
		v.SoftwareState = app.SoftwareState
		v.BindingType = app.BindingType
		v.BindingdelType = app.BindingdelType
		v.BindingdelTime = app.BindingdelTime
		v.RestrictRegtime = app.RestrictRegtime
		v.RestrictRegnum = app.RestrictRegnum
		v.Remote = app.Remote
		v.Encrypt = app.Encrypt
		v.SKey = this.RndStr(40)
		v.GivePoint = 0
		v.DefinedEncrypt = ""
		v.MenberId = 1
		if _, err = o.Insert(&v); err != nil {
			this.ErrorJson(tool.DBERROR, "添加应用失败")
			return
		}
		this.Success("添加应用成功", v)
	}
	this.ErrorJson(tool.PARAMERR, "参数错误"+err.Error())
}

func (this *SoftController) UpdateApp() {
	var app AppParam
	var err error
	if err = json.Unmarshal(this.Ctx.Input.RequestBody, &app); err == nil {
		o := orm.NewOrm()
		v := models.BxSoftware{}
		v.Name = app.Name
		if err = o.Read(&v, "name"); err != nil {
			this.ErrorJson(tool.SOFTWAREERR, "该应用不存在")
			return
		}
		v.Name = app.Name
		v.HeartTime = app.HeartTime
		v.Version = app.Version
		v.Notice = app.Notice
		v.StaticData = app.StaticData
		v.GiveTime = app.GiveTime
		v.LoginType = app.LoginType
		v.UpdateData = app.UpdateData
		v.UpdateType = app.UpdateType
		v.TrialInterval = app.TrialInterval
		v.TrialData = app.TrialData
		v.SoftwareState = app.SoftwareState
		v.BindingType = app.BindingType
		v.BindingdelType = app.BindingdelType
		v.BindingdelTime = app.BindingdelTime
		v.RestrictRegtime = app.RestrictRegtime
		v.RestrictRegnum = app.RestrictRegnum
		v.Remote = app.Remote
		v.Encrypt = app.Encrypt
		if _, err = o.Update(&v); err != nil {
			this.ErrorJson(tool.DBERROR, "添加应用失败")
			return
		}
		this.Success("添加应用成功", v)
	}
	this.ErrorJson(tool.PARAMERR, "参数错误"+err.Error())
}
